<template>
  <div class="modal-form-container">
    <vuescroll :ops="ops">
      <Form ref="info" :model="infos" :rules="rules" :label-width="100">
        <FormItem :label="$t('nm-H-HADPIPxcd4ShO9ez')" prop="name">
          <Input v-model="infos.name" />
        </FormItem>
        <FormItem :label="$t('agt')">
          <Input v-model="infos.parentName" disabled/>
        </FormItem>
        <FormItem :label="$t('LTufXFh4arUbvyKe5Gxeu')">
          <Input v-model="infos.desc" type="textarea" :rows="6" />
        </FormItem>
        <FormItem>
          <Button type="primary" @click="save">{{$t('q-FETLOZVSMbWnrNq0vj6')}}</Button>
          <Button style="margin-left: 8px" @click="close">{{$t('2LFpg56tAgKyBB40xpv5l')}}</Button>
        </FormItem>
      </Form>
    </vuescroll>
  </div>
</template>

<script>
import vuescroll from 'vuescroll'
import {
  updateAdminRoleList,
  addAdminRoleList,
  addUserRole,
  updateUserRole
} from '@/api/data2'

export default {
  name: 'login_form_info',
  components: {
    vuescroll
  },
  props: {
    data: Object,
    status: String,
    rules: Object,
    open: Boolean
  },
  data () {
    return {
      ops: {
        scrollPanel: {
          maxHeight: 500
        }
      },
      infos: _.cloneDeep(this.data)
    }
  },
  watch: {
    open (val) {
      if (val) {
        this.infos = _.cloneDeep(this.data)
      }
    }
  },
  computed: {
    isAdmin () {
      return this.$store.getters.isAdmin
    },
    isAgent () {
      return this.$store.getters.isAgent
    },
    isStation () {
      return this.$store.getters.isStation
    }
  },
  methods: {
    close () {
      this.$emit('close')
    },
    async save () {
      this.$refs.info.validate(async valid => {
        if (valid) {
          const infos = { ...this.infos, menuidlist: this.infos.menuidlist.map(info => info.id) }
          let code_ = null
          let data_ = null
          let desc_ = null
          if (this.isAdmin) {
            const { code, data, desc } =
              this.status === 'new'
                ? await addAdminRoleList({ ...infos })
                : await updateAdminRoleList({ ...infos })
            code_ = code
            data_ = data
            desc_ = desc
          } else {
            const { code, data, desc } =
              this.status === 'new'
                ? await addUserRole({ ...infos })
                : await updateUserRole({ ...infos })
            code_ = code
            data_ = data
            desc_ = desc
          }
          if (code_ == 200) {
            this.$emit('success', desc_)
          } else {
            this.$emit('fail', desc_)
          }
        } else {
          this.$Message.error(this.$t('fcmS3YBVNyvO3V68Qt3xl'))
        }
      })
    }
  }
}
</script>
